package com.samoilovich.belarustoday.adapters;

import android.app.Activity;
import android.graphics.Bitmap;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.FailReason;
import com.nostra13.universalimageloader.core.assist.ImageLoadingListener;
import com.samoilovich.belarustoday.Constants;
import com.samoilovich.belarustoday.R;
import com.samoilovich.belarustoday.datemodel.EventModel;
import com.samoilovich.belarustoday.tools.Utility;

import java.util.ArrayList;

public class EventAdapter extends BaseAdapter implements ImageLoadingListener{
    private Activity mContext;
    private ArrayList<EventModel> mEvents;
    private int mLayoutRes;
    private boolean mPictureLoadingState;

    public EventAdapter(Activity context, ArrayList<EventModel> events, int layoutRes) {
        mContext = context;
        mEvents = events;
        mLayoutRes = layoutRes;
        mPictureLoadingState = Utility.getPictureLoadingState(mContext);
    }

    public int getCount() {
        if (mEvents != null) {
            return mEvents.size();
        }

        return 0;
    }

    public EventModel getItem(int position) {
        if (mEvents != null && mEvents.size() > position) {
            return mEvents.get(position);
        }

        return null;
    }

    public long getItemId(int position) {
        return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        EventModel currentItem;
        Integer pos;

        pos = Integer.valueOf(position);
        currentItem = mEvents.get(position);
        if (convertView == null) {
            convertView = mContext.getLayoutInflater().inflate(mLayoutRes, null);
            holder = new ViewHolder();
            holder.LogoWait = (ProgressBar) convertView.findViewById(R.id.progressBar_waitLogo);
            holder.Name = (TextView) convertView.findViewById(R.id.textView_eventTitle);
            holder.Place = (TextView) convertView.findViewById(R.id.textView_placeName);
            holder.Time = (TextView) convertView.findViewById(R.id.textView_time);
            holder.Logo = (ImageView) convertView.findViewById(R.id.imageView_logo);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.Logo.setTag(holder.LogoWait);
        holder.LogoWait.setTag(pos);
        if (!currentItem.getImage().equals(Constants.EMPTY_STRING)) {
            if (currentItem.getImageDrawable() != null && currentItem.getImageDrawable().get() != null) {
                holder.Logo.setVisibility(View.VISIBLE);
                holder.Logo.setScaleType(ImageView.ScaleType.CENTER_CROP);
                holder.LogoWait.setVisibility(View.GONE);
                holder.Logo.setImageDrawable(currentItem.getImageDrawable().get());
            } else {
                if (mPictureLoadingState) {
                    holder.Logo.setVisibility(View.GONE);
                    holder.LogoWait.setVisibility(View.VISIBLE);
                    ImageLoader.getInstance().denyNetworkDownloads(false);
                    ImageLoader.getInstance().displayImage(currentItem.getImage(), holder.Logo, this);
                } else {
                    holder.Logo.setVisibility(View.GONE);
                    holder.LogoWait.setVisibility(View.VISIBLE);
                    ImageLoader.getInstance().denyNetworkDownloads(true);
                    ImageLoader.getInstance().displayImage(currentItem.getImage(), holder.Logo, this);
                }
            }
        } else {
            holder.Logo.setImageResource(R.drawable.no_image);
            holder.Logo.setVisibility(View.VISIBLE);
            holder.Logo.setScaleType(ImageView.ScaleType.FIT_XY);
            holder.LogoWait.setVisibility(View.GONE);
        }
        holder.Name.setText(currentItem.getName());
        holder.Place.setText(currentItem.getPlaceName());
        holder.Time.setText(currentItem.getTime());
        convertView.setTag(holder);

        return convertView;
    }

    public void setPictureLoadingState(boolean state) {
        mPictureLoadingState = state;
    }

    public void setEvents(ArrayList<EventModel> events) {
        mEvents = events;
        notifyDataSetChanged();
    }

    @Override
    public void onLoadingStarted(String imageUri, View view) {
        view.setVisibility(View.GONE);
        ((View) view.getTag()).setVisibility(View.VISIBLE);
    }

    @Override
    public void onLoadingFailed(String imageUri, View view, FailReason failReason) {
        ImageView image = (ImageView) view;
        View viewTag = (View) view.getTag();

        viewTag.setVisibility(View.GONE);
        image.setImageResource(R.drawable.no_image);
        image.setVisibility(View.VISIBLE);
        image.setScaleType(ImageView.ScaleType.FIT_XY );
    }

    @Override
    public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
        ImageView image = (ImageView) view;
        View viewTag = (View) view.getTag();
        int position = (Integer) viewTag.getTag();

        viewTag.setVisibility(View.GONE);
        image.setVisibility(View.VISIBLE);
        image.setScaleType(ImageView.ScaleType.CENTER_CROP);
        mEvents.get(position).setImageDrawable(image.getDrawable());
    }

    @Override
    public void onLoadingCancelled(String imageUri, View view) {
    }

    // -----------------------------------------------------------------------------------
    private class ViewHolder {
        ProgressBar LogoWait;
        ImageView Logo;
        TextView Name;
        TextView Place;
        TextView Time;
    }

}
